package com.longge;

public class NumRookCaptures {
    public int numRookCaptures(char[][] board) {

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == 'R') {
                    int ans = 0;
                    int tempJ = j;
                    while (tempJ - 1 >= 0) {
                        tempJ--;
                        if (board[i][tempJ] == 'B') break;
                        if (board[i][tempJ] == 'p') {
                            ans++;
                            break;
                        }
                    }
                    tempJ = j;
                    while (tempJ + 1 < board[0].length) {
                        tempJ++;
                        if (board[i][tempJ] == 'B') break;
                        if (board[i][tempJ] == 'p') {
                            ans++;
                            break;
                        }
                    }

                    int tempI = i;
                    while (tempI + 1 < board.length) {
                        tempI++;
                        if (board[tempI][j] == 'B') break;
                        if (board[tempI][j] == 'p') {
                            ans++;
                            break;
                        }
                    }
                    tempI = i;
                    while (tempI - 1 >= 0) {
                        tempI--;
                        if (board[tempI][j] == 'B') break;
                        if (board[tempI][j] == 'p') {
                            ans++;
                            break;
                        }
                    }
                    return ans;
                }
            }
        }
        return 0;
    }
}
